package gmusic.command;

import gmusic.bo.ArtistBean;
import gmusic.bo.ReleaseBean;
import gmusic.bo.TrackBean;
import gmusic.services.impl.GMusicServicesImpl;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import algutil.msg.Message;

public class ModifierMp3Pre implements Command {

	// private final static Logger log = Logger.getLogger(ModifierMp3.class);

	public String execute(HttpServletRequest req) throws Exception {

		HttpSession session = req.getSession();
		Message msg = null;

		// Provient de ajouterTag.jsp
		String rgmidS = (String) req.getParameter("rgmid");

		// Provient de la liste des chansons
		ArtistBean artist = (ArtistBean) session.getAttribute("groupe");
		ReleaseBean release = (ReleaseBean) session.getAttribute("album");
		List<TrackBean> tracks = (List<TrackBean>) session
				.getAttribute("chansons");

		if (rgmidS != null) {
			int rgmid = Integer.parseInt(rgmidS);
			release = GMusicServicesImpl.getInstance()
					.getReleaseSearchWithGmid(rgmid);
			artist = GMusicServicesImpl.getInstance().getArtistSearchWithGmid(
					release.getArtistGmid());
			tracks = GMusicServicesImpl.getInstance().getTracksSearchWithGmid(
					rgmid);
		}

		String cheminS = req.getParameter("chemin");
		File chemin = new File(cheminS);
		Map<TrackBean, File> map = null;

		try {
			map = GMusicServicesImpl.getInstance()
					.checkBeforeSetIdTagToMp3File(artist, tracks, chemin);
		} catch (Exception e) {
			msg = new Message(e.getMessage(), Message.MESSAGE_ERREUR);
		}

		session.setAttribute("groupe", artist);
		session.setAttribute("album", release);
		session.setAttribute("chansons", tracks);
		if (msg != null) {
			session.setAttribute("message", msg);
			return "/afficherChansonsAlbum.jsp";
		} else {
			session.setAttribute("chemin", chemin);
			session.setAttribute("map", map);
			return "/confirmModifMp3.jsp";
		}
	}
}